/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.easoftware.code.beans.cargas.threading;

import java.io.Serializable;
import java.util.List;
import net.easoftware.code.dao.DSManager;
import net.easoftware.code.dao.PersistenceEjb;
import org.apache.log4j.Logger;

/**
 *
 * @author eliu
 */
public class CargasSavingThread implements Runnable, Serializable {

    Logger logger = Logger.getLogger(CargasSavingThread.class.getName());
    private PersistenceEjb ejb;
    private DSManager dsManager;
    private List filas;
    private Integer id;
    private boolean running;
    private Integer filasProcesadas = 0;

    public CargasSavingThread(PersistenceEjb ejb, DSManager dsManager, List filas, Integer id) {
        this.ejb = ejb;
        this.filas = filas;
        this.id = id;
        this.dsManager = dsManager;
    }

    public boolean isRunning() {
        return running;
    }

    public void setRunning(boolean running) {
        this.running = running;
    }

    public Integer getFilasProcesadas() {
        return filasProcesadas;
    }

    public void setFilasProcesadas(Integer filasProcesadas) {
        this.filasProcesadas = filasProcesadas;
    }

    @Override
    public void run() {
        this.running = true;
        logger.info("Hilo de salvamento -> " + this.id);
        try {
            for (Object fila : filas) {
                ejb.guardar(fila);
                filasProcesadas++;
                System.out.println(filasProcesadas);
            }
            /*   NamedStoredProcedureQuery sp = filas.get(0).getClass().getAnnotation(NamedStoredProcedureQuery.class);
             if (sp != null) {
             QueryRunner qr = new QueryRunner(dsManager.getDs());
             qr.update("CALL " + sp.name() + "();");
             }*/
        }
        catch (Exception e) {
            logger.error("Error guardando fila... " + this.id, e);
        }
        this.running = false;
    }
}
